home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Talks & Papers / Timothy Knox / Help / Help Files / Miscellaneous / Examples < prev    next >
Text File  |  1994-06-24  |  11KB  |  350 lines

  1. {••••••••••••••••••• VARIOUS EXAMPLES FOR HELP Rel 1.4 ••••••••••••••••••••••}
  2.  
  3. {Iterative Fibonnacci U0=0 U1=1, with a local function}
  4. {•••••••••••••••••••••••••••••••••••••••••••••••••••••}
  5.  
  6. (define (fib m)
  7.   (letrec [((fi n n1 n2)                      
  8.             (cond (zero? n) (+ n1 n2)
  9.                   (fi (1- n) n2 (+ n1 n2))))]
  10.           (fi (- m 2) 1 1)))
  11.  
  12. (fib 100)
  13. { = 573147844013817084101 }
  14.  
  15. {Recursive Fibonnacci}
  16. {••••••••••••••••••••}
  17.  
  18. (define (f n)
  19.   (cond (<? n 2) 1
  20.   (+ (f (1- n)) (f (- n 2)))))
  21.  
  22. (f 20)
  23. { = 10946 }
  24.  
  25. {Surface Remove}
  26. {••••••••••••••}
  27.  
  28. (define (remove a l)
  29.      (cond (null? l) ()
  30.            (eq? a (0 l)) (remove a (-1 l))
  31.            (cons (0 l) (remove a (-1 l)))))
  32.  
  33. (remove 'a '(b c d a d e d a b))
  34. { = (b c d d e d b) }
  35.  
  36. {Count how many ways to distribute s using coin types list c}
  37. {••••••••••••••••• from Abelsson and Sussman •••••••••••••••}
  38.  
  39. (define (cat s c)
  40.        (cond (null? c) 0
  41.              (zero? s) 1
  42.              (<? s (0 c)) (cat s (-1 c))
  43.              (+ (cat s (-1 c))
  44.                       (cat (- s (0 c)) c))))
  45.  
  46. (cat 27 '(1 2 5 10))
  47. { = 76 }
  48.  
  49. {Physical Reverse}
  50. {••••••••••••••••}
  51.  
  52. (define (nreverse ll)
  53.   (letrec [((nrevaux l p)
  54.             (cond (null? (-1 l)) (cdr=! l p)
  55.                   (nrevaux (-1 l)(cdr=! l p))))]
  56.           (nrevaux ll '())))
  57.  
  58. (nreverse '(a b c d))
  59. { = (d c b a) }
  60.  
  61. {Compose functions}
  62. {•••••••••••••••••}
  63.  
  64. (define (o f g) 
  65.         (lambda x (f (apply g x))))
  66.  
  67. (define 2+ (o 1+ 1+))
  68.  
  69. (2+ 2)
  70. { = 4 }
  71.  
  72. {Various list or stream related operations}
  73. {•••••••••••••••••••••••••••••••••••••••••}
  74.  
  75. {••• Every element of l satisfies p •••}
  76.  
  77. (define (every p l)
  78.       (cond (null? l) ()
  79.             (null? (-1 l)) (p (0 l))
  80.             (p (0 l))(every p (-1 l))))
  81.  
  82. (every keyword? '(lambda cond =! let letrec))
  83. { = letrec }
  84.  
  85. {••• Some element of l satisfies p •••}
  86.  
  87. (define (some p l)
  88.       (cond (null? l) ƒ
  89.             (p (0 l)) (0 l)
  90.             (some p (-1 l))))
  91.  
  92. (some number? '(a d %1101 "azertyiop" 12 lambda))
  93. { = 12 }
  94.  
  95. {••• All the integers •••}
  96. {••••••••••••••••••••••••}
  97.  
  98. (define n (… 0))
  99.  
  100. n
  101. { = (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 …) }
  102.  
  103. {••• All even integers : sum each integer with itself •••}
  104.  
  105. (define even (map (∞ +) n n))
  106.  
  107. even
  108. { = (0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198 …) }
  109.  
  110. {••• All odd integers : get rid of the even ones •••}
  111.  
  112. (define odd (diff n even))
  113.  
  114. odd
  115. { = (1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 187 189 191 193 195 197 199 …) }
  116.  
  117. {The list of all Fibonnacci numbers: the nth}
  118. { number is the sum of the two previous ones}
  119. {•••••••••••••••••••••••••••••••••••••••••••}
  120.  
  121. (define fibn1 
  122.   (cons 1 (cons 1 (map (∞ +) fibn1 (-1 fibn1)))))
  123.  
  124. fibn1
  125. { = (1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 2971215073 4807526976 7778742049 12586269025 20365011074 32951280099 53316291173 86267571272 139583862445 225851433717 365435296162 591286729879 956722026041 1548008755920 2504730781961 4052739537881 6557470319842 10610209857723 17167680177565 27777890035288 44945570212853 72723460248141 117669030460994 190392490709135 308061521170129 498454011879264 806515533049393 1304969544928657 2111485077978050 3416454622906707 5527939700884757 8944394323791464 14472334024676221 23416728348467685 37889062373143906 61305790721611591 99194853094755497 160500643816367088 259695496911122585 420196140727489673 679891637638612258 1100087778366101931 1779979416004714189 2880067194370816120 4660046610375530309 7540113804746346429 12200160415121876738 19740274219868223167 31940434634990099905 51680708854858323072 83621143489848422977 135301852344706746049 218922995834555169026 354224848179261915075 …) }
  126.  
  127.  
  128. {A strange sequence: A fixpoint for shuffling with integers}
  129. {••••••••••••••••••••••••••••••••••••••••••••••••••••••••••}
  130.  
  131. {••• A shuffling function FOR STREAMS (no (null? l1) test) •••}
  132.  
  133. (define (entrelace l1 l2)
  134.       (cons (0 l1) (cons (0 l2) (entrelace (-1 l1) (-1 l2)))))
  135.  
  136. {••• The sequence itself •••}
  137.  
  138. (define Strange (entrelace (… 0) Strange))
  139.  
  140. Strange
  141. { = (0 0 1 0 2 1 3 0 4 2 5 1 6 3 7 0 8 4 9 2 10 5 11 1 12 6 13 3 14 7 15 0 16 8 17 4 18 9 19 2 20 10 21 5 22 11 23 1 24 12 25 6 26 13 27 3 28 14 29 7 30 15 31 0 32 16 33 8 34 17 35 4 36 18 37 9 38 19 39 2 40 20 41 10 42 21 43 5 44 22 45 11 46 23 47 1 48 24 49 12 …) }
  142.  
  143. {Input Stream}
  144. {••••••••••••}
  145.  
  146. (define (in)(cons (read)(in)))
  147.  
  148. (define input (in))
  149.  
  150. {Factorial using Continuation Passing Style}
  151. {••••••••••••••••••••••••••••••••••••••••••}
  152.  
  153. (define (fact x k)
  154.      (cond (zero? x)(k 1)
  155.      (fact (- x 1)(lambda(n)(k (* x n))))))
  156.  
  157. (fact 5 I)
  158. { = 120 }
  159.  
  160. {Factorial stream}
  161. {••••••••••••••••}
  162.  
  163. (define factl (cons 1 (map (∞ *) (… 1) factl)))
  164.  
  165. factl
  166.  
  167. {Erathostène filter}
  168. {••••••••••••••••••}
  169.  
  170. (define (erat l)
  171.   (cons (0 l) 
  172.         (erat 
  173.            (suchas (lambda(x) (not (zero?(modulo x (0 l))))) (-1 l)))))
  174.  
  175. (define primes (erat (… 2)))
  176.  
  177. primes
  178. { = (2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 …) }
  179.  
  180. {Another reverse via a fix point def.}
  181. {••••••••••••••••••••••••••••••••••••}
  182.  
  183. (define arg '(a b c d))
  184.  
  185. (define rev (cons () (map (∞ cons) arg rev)))
  186.  
  187. ((length arg) rev)
  188. { = (d c b a) }
  189.  
  190. {Hanoi}
  191. {•••••}
  192.  
  193. (define (hanoi n dep ar etap)
  194.      (cond (=? n 1)(deplacer dep ar)
  195.            (begin (hanoi (- n 1) dep etap ar)
  196.                   (deplacer dep ar)
  197.                   (hanoi (- n 1) etap ar dep))))
  198.  
  199. (define (deplacer x y)
  200.      (prin "From ")(prin x) (prin " to ") (print y))
  201.  
  202. (hanoi 3 'a 'b 'c)
  203. From a to b
  204. From a to c
  205. From b to c
  206. From a to b
  207. From c to a
  208. From c to b
  209. From a to b
  210. { = ? }
  211.  
  212. {The paradoxical combinator}
  213. {••••••••••••••••••••••••••}
  214.  
  215. ;by Church Y0
  216. (define (Y0 g)
  217.   ((lambda(x) (G (x x)))(lambda(x)(G (x x)))))
  218.  
  219. {The fixed point for fixed point combinator}
  220. {••••••••••••••••••••••••••••••••••••••••••}
  221.  
  222. (define (G y)
  223.   (lambda(f) (f (y f))))
  224.  
  225. ;by Turing Y1=Y0 G
  226. (define Y1 
  227.   ((lambda(a)
  228.     (lambda(b) (b ((a a) b)))) (lambda(a)
  229.                                  (lambda(b) (b ((a a) b))))))
  230. ;Another simple one by Klop
  231. (define £
  232.  (lambda(a)
  233.   (lambda(b)
  234.    (lambda(c)
  235.     (lambda(d)
  236.      (lambda(e)
  237.       (lambda(f)
  238.        (lambda(g)
  239.         (lambda(h)
  240.          (lambda(i)
  241.           (lambda(j)
  242.            (lambda(k)
  243.             (lambda(l)
  244.              (lambda(m)
  245.               (lambda(n)
  246.                (lambda(o)
  247.                 (lambda(p)
  248.                  (lambda(q)
  249.                   (lambda(s)
  250.                    (lambda(t)
  251.                     (lambda(u)
  252.                      (lambda(v)
  253.                       (lambda(w)
  254.                        (lambda(x)
  255.                         (lambda(y)
  256.                          (lambda(z)
  257.                           (lambda(r)
  258. (r((((((((((((((((((((((((((t h)i)s)i)s)a)f)i)x)e)d)p)o)i)n)t)c)o)m)b)i)n)a)t)o)r)))))))))))))))))))))))))))))
  259.  
  260. (define $ (((((((((((((((((((((((((£ £)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£))
  261.  
  262. ;Another one "en VF"
  263. (define £
  264.  (lambda(a)
  265.   (lambda(b)
  266.    (lambda(c)
  267.     (lambda(e)
  268.      (lambda(f)
  269.       (lambda(g)
  270.        (lambda(h)
  271.         (lambda(i)
  272.          (lambda(j)
  273.           (lambda(k)
  274.            (lambda(l)
  275.             (lambda(m)
  276.              (lambda(n)
  277.               (lambda(o)
  278.                (lambda(p)
  279.                 (lambda(q)
  280.                  (lambda(r)
  281.                   (lambda(s)
  282.                    (lambda(t)
  283.                     (lambda(u)
  284.                      (lambda(v)
  285.                       (lambda(w)
  286.                        (lambda(x)
  287.                         (lambda(y)
  288.                          (lambda(z)
  289.                           (lambda(d)
  290. (d((((((((((((((((((((((((((H e)l)p) E)s)t) T)e)r)r)i)b)l)e)m)e)n)t) F)l)e)m)m)a)r)d)))))))))))))))))))))))))))))
  291.  
  292. (define $ (((((((((((((((((((((((((£ £)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£)£))
  293.  
  294. ;Factorial associated functionnal, then factorial itself
  295. (define (FF f)
  296.   (lambda(x)
  297.     (cond (zero? x) 1
  298.           (* (f (1- x)) x))))
  299.  
  300. (define fact2 (Y1 FF))
  301.  
  302. (fact2 5)
  303. { = 120 }
  304.  
  305. {Use primes list l to decompose number n}
  306. {•••••••••••••••••••••••••••••••••••••••}
  307.  
  308. (define (dec n l)
  309.   (cond (=? n 1) ()
  310.         (zero? (modulo n (0 l))) (cons (0 l) (dec (/ n (0 l)) l))
  311.         (<? n (* (0 l)(0 l))) (list n)
  312.         (dec n (-1 l))))
  313.  
  314. (dec 123469 primes)
  315. { = (37 47 71) }
  316.  
  317. {A Quick Sort}
  318. {••••••••••••}
  319.  
  320. (define (sortby f l)
  321.   (letrec [((splitby x part ll r)
  322.             (cond (null? x)(cons ll r)
  323.                   (<? (f (0 x))(f part)) 
  324.                     (splitby f (-1 x) part (cons (0 x) ll) r)
  325.                   (splitby (-1 x) part ll (cons (0 x) r))))
  326.  
  327.            ((sortit lr)(append (sortby f (0 lr))
  328.                                (append (list (0 l)) (sortby f (-1 lr)))))]
  329.           (cond (null? l) ()
  330.                 (sortit (splitby (-1 l) (0 l) () ())))))
  331.  
  332. (sortby I '(0 2 4 6 5 3 1))
  333. { = (0 1 2 3 4 5 6) }
  334.  
  335. {••• A simple backward chaining horn clauses propositionnal ••••}
  336. {• prover: b is the knowledge base, a is the prop var to prove •}
  337. {• a horn clause a1…an -> b is coded by the list (b a1 … an)   •}
  338. {•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••}
  339.  
  340. (define (p a b)
  341.  (some (lambda(r) 
  342.         (if (eq? (0 r) a) (every (lambda(f) (p f b)) (-1 r)) 
  343.             ƒ)) 
  344.       b))
  345.  
  346. (p 'a '((a b c) (b e)(b)(c e)))
  347. { = ƒ }
  348. (p 'a '((a b c) (b e)(e)(c e)))
  349. { = (a b c) }
  350.